package es.udc.fbellas.j2ee.tiendaweb.model.detallespedido.DAO;

import java.sql.Connection;

import javax.sql.DataSource;

import es.udc.fbellas.j2ee.tiendaweb.model.detallespedido.TO.DetallesPedidoTO;
import es.udc.fbellas.j2ee.util.configuration.ConfigurationParametersManager;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;

/**
 * A factory to get <code>SQLAccountDAO</code> objects.
 * <p>
 * Required configuration parameters:
 * <ul>
 * <li><code>SQLAccountDAOFactory/daoClassName</code>: it must specify the full
 * class name of the class implementing <code>SQLAccountDAO</code>.</li>
 * </ul>
 */
public class SQLDetallesPedidoDAOFactory {

    private final static String DAO_CLASS_NAME_PARAMETER =
        "SQLDetallesPedidoDAOFactory/daoClassName";

    private final static Class daoClass = getDAOClass();
    
    private SQLDetallesPedidoDAOFactory() {}
    
    private static Class getDAOClass() {
    
        Class theClass = null;
    
        try {
        
            String daoClassName = 
                ConfigurationParametersManager.getParameter(
                    DAO_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(daoClassName);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return theClass;
        
    }
    
    public static SQLDetallesPedidoDAO getDAO() throws InternalErrorException {
        
        try {        
            return (SQLDetallesPedidoDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
        
    }
    
    /* 
     * Test code. Uncomment for testing. Test this class and any class
     * implementing "SQLAccountDAO".
     */         
    public static void main (String[] args) {    
        
        Connection connection = null;
    
        try {        
            
            /* Get a connection. */
            DataSource dataSource = new SimpleDataSource();
           connection = dataSource.getConnection();        
                    
            /* Get dao. */
            SQLDetallesPedidoDAO dao = SQLDetallesPedidoDAOFactory.getDAO();

            /* Test "SQLLineaPedidoDAO::create". */
            System.out.println("Test for 'SQLDetallesPedidoDAO::create'");
            DetallesPedidoTO detallesTO = new DetallesPedidoTO(94543,java.sql.Date.valueOf("2014-05-12"),1947382,"PENDIENTE","INSJVP",12345);
            detallesTO = dao.create(connection, detallesTO);
            System.out.println("detalles created => " + detallesTO);
            
            /* Test "SQLLineapedidoDAO::exists". */
            System.out.println("Test for 'SQLDetallesPedidoDAO::exist'");
            if (!dao.exists(connection, detallesTO.getIdPedido())) {
                throw new Exception("no se encuentra los detalles con identificador " +
                    "accountIdentifier = " + 
                    detallesTO.getIdPedido());
            }
            else System.out.println("Los detalles existen");

            /* Test "SQLDetallesPedidoDAO::find". */
            System.out.println("Test for 'SQLLineaPedidoDAO::find'");
            System.out.println(dao.find(connection,
                detallesTO.getIdPedido()));

            /* Test "SQLDetallesPedidoDAO::actualizaEstado". */
            System.out.println("Test for 'SQLDetallesPedidoDAO::actualizaEstado'");
            dao.actualizaEstado(connection,98544);
            System.out.println("Linea Pedido actualizada");
            System.out.println("----------------------------------------");

            

            
            /* Tests OK. */
            System.out.println("Tests OK !!!!");
        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                GeneralOperations.closeConnection(connection);
            } catch (InternalErrorException e) {
                e.printStackTrace();
            }
        }
               
    }    
    
}
